<template>
    <el-main>
        <el-row :gutter="20">
            <el-card class="box-card" style="min-height: 200px;">
                <avue-crud ref="crud" :data="questionListData" :option="optionQuestion" @expand-change="expandChanges"
                    :page="page" @refresh-change="refresh" @size-change="sizeChange" @current-change="currentChange"
                    @search-change="searchChange" @search-reset="searchReset">
                    <template slot="expand" slot-scope="{row}">
                        <avue-article :option="questionDetailOption" :data="questionDetailData"></avue-article>
                    </template>
                    <template slot-scope="scope" slot="menu">
                        <el-button :size="scope.size" :type="scope.type" v-if="scope.row.is_cream === 1"
                            @click="handleSetCream(scope.row, 0)"><span style="color:#13ce66">下架精华</span></el-button>
                        <el-button :size="scope.size" :type="scope.type" v-if="scope.row.is_cream === 0"
                            @click="handleSetCream(scope.row, 1)">设为精华</el-button>
                    </template>
                </avue-crud>
            </el-card>
        </el-row>

    </el-main>
</template>

<script>
    export default {
        data() {
            return {
                questionDetailOption: {
                    props: {
                        title: 'title',
                        meta: 'meta',
                        body: 'body'
                    },
                },
                searchForm: {
                    keyword: '',
                    topic_id: '',
                },
                page: {
                    // pageSizes: [2, 10, 20, 30, 40, 100],
                    currentPage: 1,
                    total: 0,
                    pageSize: 10
                },
                questionDetailData: [],
                questionListData: [],
                optionQuestion: {
                    expand: true,
                    expandRowKeys: [],
                    idKey: 'id',
                    align: 'center',
                    menuAlign: 'center',
                    size: 'small',
                    dialogWidth: '35%',
                    dialogTop: 200,
                    page: true,
                    addBtn: false,
                    delBtn: false,
                    editBtn: false,
                    viewBtn: false,
                    searchShow: true,
                    filterBtn: false,
                    columnBtn: false,
                    searchMenuSpan: 5,
                    column: [{
                        label: '话题类型',
                        prop: 'topic_id',
                        span: 24,
                        type: 'select',
                        addDisplay: false,
                        editDisplay: false,
                        viewDisplay: false,
                        search: true,
                        hide: true,
                        dicUrl:'/api/zhihu/topicSelectDict'
                    }, {
                        label: '标题',
                        prop: 'title',
                        span: 24,
                    }, {
                        label: '分类',
                        prop: 'catesShow',
                        span: 24,
                    }, {
                        label: '所属话题',
                        prop: 'topic',
                        span: 24,
                    }, {
                        label: '发布人',
                        prop: 'publishname',
                        span: 24,
                    }, {
                        label: '喜欢/评论/关注/阅读',
                        prop: 'fclr',
                        span: 24,
                    }, {
                        label: '发布时间',
                        prop: 'created_at',
                        span: 24,
                    }, {
                        label: '关键字',
                        prop: 'keyword',
                        addDisplay: false,
                        editDisplay: false,
                        viewDisplay: false,
                        search: true,
                        span: 24,
                        width: 180,
                        hide: true,
                        searchPlaceholder: '标题'
                    }]
                },
            }
        },
        created() {
            this.getListQuestion()
        },
        methods: {
            searchChange(params, done) {
                done();
                this.searchForm.keyword = params.keyword || ''
                this.searchForm.topic_id = params.topic_id || ''
                this.getListQuestion()
            },
            searchReset() {
                this.searchForm = {}
                this.getListQuestion()
            },
            // 分页
            sizeChange(val) {
                this.page.currentPage = 1
                this.page.pageSize = val
                this.getListQuestion()
            },
            // 当前页
            currentChange(val) {
                this.page.currentPage = val
                this.getListQuestion()
            },
            // 刷新
            refresh() {
                this.getListQuestion()
            },
            getListQuestion() {
                const _this = this
                const params = {
                    keyword: _this.searchForm.keyword,
                    topic_id: _this.searchForm.topic_id,
                    page: _this.page.currentPage,
                    limit: _this.page.pageSize,
                }
                _this.axios.post(_this.$API.ZHIHUAPI.listQuestion, _this.qs.stringify(params)).then((res) => {
                    res = res.data
                    if (res.code === 10000) {
                        if (res.hasOwnProperty('data') && Object.keys(res.data).length > 0) {
                            _this.questionListData = res.data.list
                            _this.page.total = res.data.count
                        } else {
                            _this.questionListData = []
                            _this.page.total = 0
                        }
                    } else {
                        _this.$message({
                            showClose: false,
                            message: res.message,
                            type: 'error'
                        })
                        _this.questionListData = []
                        _this.page.total = 0
                    }
                }).catch(() => {
                    _this.$message({
                        showClose: false,
                        message: '网络连接失败，请重试！',
                        type: 'error'
                    })
                    _this.questionListData = []
                    _this.page.total = 0
                })
            },
            expandChanges(row, expendList) {
                if (expendList.length) {
                    this.optionQuestion.expandRowKeys = []
                    this.questionDetailData = []
                    if (row) {
                        this.optionQuestion.expandRowKeys.push(row.id)
                        this.questionDetailData = {
                            title: row.title,
                            meta: row.publishname + " / " + row.topic + " / 阅读(" + row.read_count + ") / 关注(" + row.followers_count +") / 喜欢(" + row.favourite_count + ") / 评论(" + row.answers_count + ") / " + row.created_at,
                            body: row.content
                        }
                    }
                } else {
                    this.optionQuestion.expandRowKeys = []
                    this.questionDetailData = []
                }
            },
            handleSetCream(row, cream) {
                const _this = this
                const params = {
                    id: row.id,
                    is_cream: cream
                }
                _this.axios.post(_this.$API.ZHIHUAPI.configQuestionCream, _this.qs.stringify(params)).then((res) => {
                    res = res.data
                    if (res.code === 10000) {
                        _this.$message({
                            showClose: false,
                            message: res.message,
                            type: 'success'
                        })
                        this.getListQuestion()
                    } else {
                        _this.$message({
                            showClose: false,
                            message: res.message,
                            type: 'error'
                        })
                    }
                }).catch(() => {
                    _this.$message({
                        showClose: false,
                        message: '网络连接失败，请重试！',
                        type: 'error'
                    })
                })
            }
        }
    }
</script>

<style>
</style>
